
import { _decorator, Component, Node, SpriteFrame, Sprite, Label } from 'cc';
import Loader from '../../Script/Common/Loader';
import { SpinAwardCfg } from './SpinAwardCfg';
const { ccclass, property } = _decorator;

@ccclass('SpinRoot')
export class SpinRoot extends Component {
    @property(Node)
    goodsLayer: Node = null;

    isLoad = false;
    _spritArr: { [id: number]: SpriteFrame } = {};

    onLoad() {
        this.loadSprites();
        // this.showSpin(false);
    }

    //加载对应商品图标
    loadSprites() {
        // let url = 'Image/SpinUI/awards';
        // Loader.loadBundle("UI", () => {
        //     Loader.loadBundleDir("UI", url, (res: SpriteFrame[]) => {
        //         for (let i = 0; i < res.length; i++) {
        //             const s = res[i];
        //             let id = +s.name;
        //             this._spritArr[id] = s;
        //         }
                this.loadFinished();
        //     }, SpriteFrame, false);
        // })
    }

    loadFinished() {
        this.isLoad = true;
        //替换显示图片+数值
        for (const key in SpinAwardCfg) {
            const cfg = SpinAwardCfg[key];
            let id = +key;
            let item = this.goodsLayer.getChildByName(key);
            // item.setScale(2, 2, 2);
            // let sp = item.getComponent(Sprite);
            let label = item.getComponentInChildren(Label);
            // sp.spriteFrame = this._spritArr[cfg.sf];
            if (cfg.val > 0) {
                label.node.active = true;
                label.string = cfg.val + '';
            } else {
                label.node.active = false;
            }
        }
        // this.showSpin(true);
    }
    //显示子节点
    showSpin(isShow) {
        this.node.children.forEach(e => e.active = isShow);
    }
}

